//
//  ZPStatusRetweetView.swift
//  ZPWeibo
//
//  Created by zhangpeng on 16/4/21.
//  Copyright © 2016年 zhangpeng. All rights reserved.
//

import UIKit
import SnapKit

class ZPStatusRetweetView: UIView {

    // 记录我们转发微博的底部约束
    var retweetViewConstraint: Constraint?
    
    var status:ZPStatus?{
    
        didSet{
        
            //赋值
            
            contentLabel.text = status?.retweeted_status?.text
              // 先要写在我们的转发微博底部约束
            self.retweetViewConstraint?.uninstall()
            
            /*
            判断是否有转发微博配图
            - status?.retweeted_status?.pic_urls?.count != 0  代表有配图
            - status?.retweeted_status?.pic_urls?.count == 0  代表没有配图
            */
            // 有转发微博配图
            if status?.retweeted_status?.pic_urls?.count != 0{
            
                // 给配图设置数据
                pictureView.pictureInfo = status?.retweeted_status?.pic_urls
                
                //更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.retweetViewConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                
                //显示配图
                pictureView.hidden = false
                
            }else{
            
                // 没有配图
                //更新约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.retweetViewConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                
                //显示配图
                pictureView.hidden = true
                
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    
    }
    
    // MARK: - 设置视图
    private func setupUI(){
    
        self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(10)
            make.width.equalTo(SCREENW - 20)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo(contentLabel)
        }
        
        
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewConstraint = make.bottom.equalTo(pictureView.snp_bottom).offset(10).constraint
        }
    }
    
    
    // MARK: - 懒加载控件
    // 转发微博正文
    
    private lazy var contentLabel:UILabel = {
    
        let lab = UILabel(textColor: UIColor.blackColor(), textFont: 14)
        lab.numberOfLines = 0
        
        return lab
    }()
     // 转发微博配图
    
    private lazy var pictureView:ZPStatusPictureView = ZPStatusPictureView()
}
